<?php
namespace Admin\Controller;
use Think\Controller;
class AppController extends CommonController {

    public function index()
    {
        self::lists();
    }

    public function lists()
    {
        $Modle      =   M('app'); // 实例化User对象
        $count      =   $Modle->count();// 查询满足要求的总记录数
        $Page       =   new \Think\Page($count,50);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       =   $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性

        //$field      =   array('id','logo','appid','secret','url','envversion','content','title','isshow','category','h5show','sort','module','user_table','template_table','other_name');
        $field      =   array('id','logo','appid','secret','url','envversion','content','title','isshow','sort','module','user_table','template_table','other_name','template_id');
        $data['list'] = $Modle->field($field)->limit($Page->firstRow.','.$Page->listRows)->select();

        $this->assign('page',$show);        // 赋值分页输出
        $this->assign("data",$data);
        $this->display('lists');
    }

    public function edit()
    {
        if (IS_AJAX)
        {
            $where['id']  =   I('post.id','','htmlspecialchars');
            if (empty($where['id']))
            {
                $msg['state']   =   -1;
                $msg['error']   =   '非法请求';
                $this->ajaxReturn ($msg,'JSON');
            }

            $data['appid']     =   I('post.appid','','htmlspecialchars');
            $data['secret']    =   I('post.secret','','htmlspecialchars');
            $data['url']       =   I('post.url','','htmlspecialchars');
            $data['envversion']=   I('post.envversion','','htmlspecialchars');
            $data['content']   =   I('post.content','','htmlspecialchars');
            $data['isshow']    =   I('post.isshow','','htmlspecialchars');
            $data['title']     =   I('post.title','','htmlspecialchars');
            $data['sort']      =   I('post.sort','','htmlspecialchars');

            $data['module']             =   I('post.module','','htmlspecialchars');
            $data['user_table']         =   I('post.user_table','','htmlspecialchars');
            $data['template_table']     =   I('post.template_table','','htmlspecialchars');
            $data['other_name']         =   I('post.other_name','','htmlspecialchars');
            $data['template_id']        =   I('post.template_id','','htmlspecialchars');

            $result     =   M('app')->where($where)->save($data); // 根据条件更新记录

            //记录相关操作日志

            if (!empty($result))
            {
                $msg['state']   =   200;
                $msg['error']   =   '更新成功';
                $this->ajaxReturn ($msg,'JSON');
            }
            else
            {
                $msg['state']   =   -1;
                $msg['error']   =   '更新失败';
                $this->ajaxReturn ($msg,'JSON');
            }
        }
        else
        {
            $where['id']  =   I('get.id','','htmlspecialchars');

            if (empty($where['id']))
            {
                $this->error('非法请求');
            }

            $field      =   array('id','logo','appid','secret','url','envversion','content','title','isshow','sort','module','user_table','template_table','other_name','template_id');
            $data['app_info']  =   M('app')->where($where)->field($field)->find();

            if ( empty($data))
            {
                $this->error('数据不存在');
            }

            $this->assign("data",$data);
            $this->display('edit');
        }
    }

    public function add()
    {
        if (IS_AJAX)
        {
            $data['appid']     =   I('post.appid','','htmlspecialchars');
            $data['secret']    =   I('post.secret','','htmlspecialchars');
            $data['url']       =   I('post.url','','htmlspecialchars');
            $data['envversion']=   I('post.envversion','','htmlspecialchars');
            $data['content']   =   I('post.content','','htmlspecialchars');
            $data['isshow']    =   I('post.isshow','','htmlspecialchars');
            $data['title']     =   I('post.title','','htmlspecialchars');
            $data['sort']      =   I('post.sort','','htmlspecialchars');
            $data['module']             =   I('post.module','','htmlspecialchars');
            $data['user_table']         =   I('post.user_table','','htmlspecialchars');
            $data['template_table']     =   I('post.template_table','','htmlspecialchars');
            $data['other_name']         =   I('post.other_name','','htmlspecialchars');
            $data['template_id']        =   I('post.template_id','','htmlspecialchars');

            $result     =   M('app')->add($data);

            if (!empty($result))
            {
                $msg['state']   =   200;
                $msg['error']   =   '添加成功';
                $this->ajaxReturn ($msg,'JSON');
            }
            else
            {
                $msg['state']   =   -1;
                $msg['error']   =   '添加失败';
                $this->ajaxReturn ($msg,'JSON');
            }

        }
        else
        {
            $this->display('add');
        }
    }
}